﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Stacky;
using System.Configuration;

namespace StackBookMvc.Models
{
    public class BaseViewModel
    {
        protected Facebook.Session.FacebookSession mSession = null;
        protected Facebook.Rest.Api mApi = null;
        protected Stacky.User mUser = null;
        protected IEnumerable<Stacky.User> mSearchResults = null;
        protected StackBookLib.Interfaces.IStackBookRespository mRepository = null;

        public BaseViewModel(Facebook.Session.FacebookSession oSession)
            : this(oSession, new StackBookL2S.L2SStackBookRepository())
        {
        }

        public BaseViewModel(Facebook.Session.FacebookSession oSession, StackBookLib.Interfaces.IStackBookRespository oRepository)
        {
            mSession = oSession;
            mRepository = oRepository;
        }

        public Facebook.Rest.Api FacebookApi
        {
            get
            {
                return new Facebook.Rest.Api(mSession);
            }
        }

        public Stacky.User StackOverflowUser
        {
            get
            {
                if (mUser == null)
                {
                    var api = FacebookApi;
                    if (api != null)
                    {
                        var item = mRepository.GetByFacebookId(api.Session.UserId);
                        if (item != null)
                        {
                            if (item.StackOverflowId > 0)
                            {
                                IUrlClient urlClient = new UrlClient();
                                IProtocol protocol = new JsonProtocol();

                                var client = new StackyClient(ConfigurationManager.AppSettings["StackAppsVersion"], ConfigurationManager.AppSettings["StackAppsKey"], HostSite.StackOverflow, urlClient, protocol);
                                mUser = client.GetUser((int)item.StackOverflowId);
                            }
                        }
                    }
                }
                return mUser;
            }
        }

        public StackBookLib.Interfaces.IStackBookRespository Repository
        {
            get
            {
                return mRepository;
            }
        }

        public IEnumerable<Stacky.User> StackOverflowSearchResults
        {
            get
            {
                return mSearchResults;
            }
            set
            {
                mSearchResults = value;
            }

        }

        public void SetFacebookStackOverflowAssociation(int stackOverflowUserId)
        {
            mRepository.SetFacebookAssociation(FacebookApi.Session.UserId, stackOverflowUserId);
        }

        public void RemoveFacebookStackOverflowAssociation()
        {
            mRepository.RemoveFacebookAssociation(FacebookApi.Session.UserId);
            mUser = null;
        }
    }
}